package com.waiter.moximoxi.utils;

import android.app.Activity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YBD-TECH029 on 2019/3/12.
 */

public class ActivityCollector {

    public static List<Activity> activities = new ArrayList<Activity>();

    /**
     * 添加某个activity
     *
     * @param activity
     */
    public static void addActivity(Activity activity) {
        if (activity != null) {
            activities.add(activity);
        }
    }

    /**
     * 移除某个activity
     *
     * @param activity
     */
    public static void removeActivity(Activity activity) {
        if (activity != null) {
            activities.remove(activity);
        }
    }

    /**
     * 关闭某个activity
     */
    public static void finishActivity(Class clazz) {
        if (clazz != null) {
            for (int i = 0; i < activities.size(); i++) {
                if (activities.get(i).getClass().getName().equals(clazz.getName())) {
                    activities.get(i).finish();
                }
            }
        }
    }

    /**
     * 关闭除clazz之外所有的activity
     */
    public static void finishOtherActivity(Class clazz) {
        if (clazz != null) {
            for (int i = 0; i < activities.size(); i++) {
                if (!activities.get(i).getClass().getName().equals(clazz.getName())) {
                    activities.get(i).finish();
                }
            }
        }

//        if (ActivityCollector.activities.size() != 0) {
//            for (int i = 0; i < ActivityCollector.activities.size(); i++) {
//                if (ActivityCollector.activities.get(i) instanceof activity) {//需要把传参改为activity
//
//                }else {
//                    ActivityCollector.activities.get(i).finish();
//                }
//            }
//        }
    }

    /**
     * 判断某个activity是否在栈内
     */
    public static boolean isOpenActivity(Class clazz) {
        if (clazz != null) {
            for (int i = 0; i < activities.size(); i++) {
                if (activities.get(i).getClass().getName().equals(clazz.getName())) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 关闭所有activity
     */
    public static void finishAll() {
        for (Activity activity : activities) {
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
    }
}
